<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
      <el-button v-if="can_sku && model.link_order==1 && model.need_sku==1" @click="to_sku(row)" size="small">规格</el-button>
    </template>
  </list_url>
</template>
<script>
import {_href} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let model = {name:this.$get('model_name'), alias:this.$get('model_alias','')};
    let controller = this.camel_str(model.name);
    if (is_del){
      if(in_right_admin(controller+'/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin(controller+'/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin(controller+'/add')) btns['添加'] = function(){ app.iframe('edit?'+app.param()) }
      if(in_right_admin(controller+'/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin(controller+'/recycle')) btns['回收站'] = function(){ app.iframe('index?'+app.param({is_del:1})) }
      if(in_right_admin(controller+'/export')) btns['导出'] = function(){
        let param = s94Mjs.param_encode(app.$refs['list_url'].$refs['forms'].form_data());
        window.open(_href("/admin/"+model.name+"/export?"+param), '_blank')
      }
    }
    return {
      model: model,
      can_sku: in_right_admin(controller+'/sku_edit'),
      config: {
        api: '',
        title: model.alias+"列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: {},
        list_cols: [],
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model.name;
      data.model_alias = this.model.alias;
      return s94Mjs.param_encode(data);
    },
    to_edit(row){
      let can_edit = in_right_admin(this.camel_str(this.model.name)+'/edit') && ~[0,row.tenant_id].indexOf(this.admin_config('this_tenant.id'))
      let param = { id: row.id }
      if (!can_edit) param['is_info'] = 1;
      this.iframe('edit?'+this.param(param))
    },
    to_sku(row){
      this.iframe('sku_edit?'+this.param({id: row.id}));
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    let field_width = {
      category_name: '80px',
      head_img: '40px',
      price: '70px',
      price_vip: '70px',
      deliver_price: '70px',
      skip_deliver: '70px',
      skip_use: '70px',
      stock: '55px',
      sales: '55px',
      sort: '55px',
      visits: '55px',
    };
    ajax({url:'/admin/'+app.model.name+'/field_config?action=list&table=0', cache:true}, function(res){
      app.model = res.data.model;
      if(app.in_right('Category/list')){
        app.config.btns[app.model.alias+'类目'] = function(){ app.iframe('/category/index?model_id='+app.model.id) }
      }
      if (app.model.link_order!=1) app.can_sku = false;
      app.config.title = app.model.alias+'列表';
      let forms_config = {
        // encode_id: {label:'编号',name:'where[encode_id][eq]',type:'text_one',value:''}
      };
      let list_cols = [
        {name:'id', label:'ID', width:'65px', fixed:'left'},
        // {name:'encode_id', label:'编号', width:'70px'}
      ];
      s94Mjs.each(res.data.where, function(v){
        let key = v.name.replace('.','_')+'_'+v.method;
        forms_config[key] = {
          label: v.label,
          name: `where[${v.name}][${v.method}]`,
          tips: v.tips,
          type: v.type,
          config: v.config,
          value: v.value,
        };
        if(/^list_ids?$/.test(v.type))forms_config[key]['no_table'] = 1;
      })
      app.config.forms_config = forms_config;
      s94Mjs.each(res.data.cols, function(v){
        let name = v.name.replace('.','_');
        let row = Object.assign({
          name: name,
          label: v.label,
          type: v.type || '',
          config: v.config,
          width: field_width[name] || '',
        }, JSON.parse(v.list_props||'[]'))
        list_cols.push(row);
      })
      let is_del = app.$get('is_del');
      if (is_del){
        list_cols.push({name:'delete_time', label:'删除时间', width:'165px', type:'datetime'});
      }else {
        list_cols.push({name:'create_time', label:'创建时间', width:'165px'});
      }
      if (!app.admin_config('this_tenant.id')){
        list_cols.push({name:'tenant_id', label:'租户', width:'65px', fixed:'right'});
      }
      list_cols.push({name:'action', label:'操作', width:'140px', type:'action', fixed:'right'});
      app.config.list_cols = list_cols;
      setTimeout(function(){
        try {
          app.$refs.list_url.$refs.forms.set_init_value();
          app.config.api = '/admin/' + app.model.name + '/' + (is_del ? 'recycle' : 'list');
          app.$refs.list_url.list();
        } catch (e) {}
      })
      return false;
    })

  }
}
</script>
